package menu;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.MenuElement;

import main.Application;
import controls.VocPanel;

/*
 * This class is the project's work horse
 */
public class MainMenu implements ActionListener {
    JTextArea output;
    JScrollPane scrollPane;
    String newline = "\n";
    JMenuBar menuBar;

    private String xmlPath;
    
    public MainMenu(String xmlPath)
    {
    	this.xmlPath = xmlPath;
    }
    
    public JMenuBar createMenuBar() {
    	try{
    	MenuAdapter menuAdapter= new MenuAdapter(xmlPath);
    	menuBar = menuAdapter.getMenuBar();
    	}
    	catch (Exception e)
    	{
    		menuBar = new JMenuBar();
    		Application.getInstance().notifyAboutException(e);
    	}
    	return menuBar;
    }

    public Container createContentPane() {
        //Create the content-pane-to-be.
        JPanel contentPane = new JPanel(new BorderLayout());
        contentPane.setOpaque(true);

        //Create a scrolled text area.
        output = new JTextArea(5, 30);
        output.setEditable(false);
        scrollPane = new JScrollPane(output);

        //Add the text area to the content pane.
        contentPane.add(scrollPane, BorderLayout.CENTER);

        return contentPane;
    }

    public void actionPerformed(ActionEvent e){
    	try {
        
        VocPanel bw = new VocPanel("test","id");	
		JInternalFrame browser = new JInternalFrame("vocTest", true, true, true, true);
		browser.getContentPane().add(bw);
		browser.setSize(bw.getSize());
		browser.setLocation(50, 50);
	   
	   browser.pack();
	   browser.setVisible(true);
	   Application.getInstance().getMainFrame().add(browser);
        
    	} catch (Exception e1) {
    		
    		e1.printStackTrace();
    	}
    }
    
    

    // Returns just the class name -- no package info.
    protected String getClassName(Object o) {
        String classString = o.getClass().getName();
        int dotIndex = classString.lastIndexOf(".");
        return classString.substring(dotIndex+1);
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = MainMenu.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
    
    /**
     * wyswietli manu az do wskazanego elementu. No pokaze cale drzewo.
     * @param menuItem
     */
    public void showParents(JMenuItem menuItem)
    {			
    	searchAndShowPathToItem(menuBar,menuItem);	
    }

    /**
     * przesmiga rekurencyjnie i wyswietli cala sciezke do wskazanego elementu
     * @param menuElement
     * @param menuItem
     * @return
     */
	private boolean searchAndShowPathToItem(MenuElement menuElement, JMenuItem menuItem) 
	{
		MenuElement[] subMenus = menuElement.getSubElements();			
		for(int i=0;i<subMenus.length;++i)
		{
			if(menuItem==subMenus[i]) //wiem,ze porownuje referencje
			{			
				//menuItem.doClick();
				return true;		
			}
			
			if(searchAndShowPathToItem(subMenus[i],menuItem))
			{
				if(subMenus[i] instanceof JMenu)
				{					
					((JMenu)subMenus[i]).doClick();						
					return false;
				}
				return true;			
			}
		}
		return false;		
	}

}
